// VRectangle.cpp
//
#include "VRectangle.h"

#include "OpenGL/Renderer.h"
#include "OpenGL/Selection.h"
#include "OpenGL/OpenGL.h"

#include <iostream>

#include "MemCheck.h"

// ------------------------------------------------------------------------------------------------
VRectangle::VRectangle()
{}

// ------------------------------------------------------------------------------------------------
VRectangle::VRectangle( const OpenGL::Math::dvec3 &bottomLeft, const OpenGL::Math::dvec3 &topRight )
: _rect(bottomLeft,topRight)
{
  _boundingBox = OpenGL::Math::box3(bottomLeft,topRight);
}

// ------------------------------------------------------------------------------------------------
VRectangle::VRectangle( const OpenGL::Math::box3 &rect )
: _rect(rect)
{
	_boundingBox = OpenGL::Math::box3(rect);
}

// ------------------------------------------------------------------------------------------------
VObject::ObjectType VRectangle::type() const		{ return Shape; }

// ------------------------------------------------------------------------------------------------
std::vector<OpenGL::Math::dvec3> VRectangle::snapPoints() const
{
	std::vector<OpenGL::Math::dvec3> pts;
	pts.push_back( OpenGL::Math::dvec3(_rect.minPoint().x,_rect.minPoint().y,0) );
	pts.push_back( OpenGL::Math::dvec3(_rect.maxPoint().x,_rect.minPoint().y,0) );
	pts.push_back( OpenGL::Math::dvec3(_rect.maxPoint().x,_rect.maxPoint().y,0) );
	pts.push_back( OpenGL::Math::dvec3(_rect.minPoint().x,_rect.maxPoint().y,0) );
	pts.push_back( _rect.center() );

	return pts;
}

// ------------------------------------------------------------------------------------------------
OpenGL::Math::dvec3 VRectangle::textPosition() const
{
	return _boundingBox.center();
}

// ------------------------------------------------------------------------------------------------
void VRectangle::draw( OpenGL::Renderer *r ) const
{
	OpenGL::Math::dvec3 o = r->origin();

	//glPushAttrib(GL_COLOR_BUFFER_BIT|GL_CURRENT_BIT|GL_LINE_BIT);

	OpenGL::Math::box3 box = _rect.translated(-o);

	// fill
	glColor4fv( color().transparent().rgba() );
	glBegin( GL_TRIANGLE_STRIP );
		glVertex2d( box.minPoint().x, box.maxPoint().y );
		glVertex2d( box.minPoint().x, box.minPoint().y );
		glVertex2d( box.maxPoint().x, box.maxPoint().y );
		glVertex2d( box.maxPoint().x, box.minPoint().y );
	glEnd();

	// borders
	glLineWidth(2.f);
	glColor4fv( color().rgba() );
	glBegin( GL_LINE_LOOP);
		glVertex2d( box.minPoint().x, box.maxPoint().y );
		glVertex2d( box.minPoint().x, box.minPoint().y );
		glVertex2d( box.maxPoint().x, box.minPoint().y );
		glVertex2d( box.maxPoint().x, box.maxPoint().y );
	glEnd();

	//glPopAttrib();
}

// ------------------------------------------------------------------------------------------------
void VRectangle::drawForSelection( OpenGL::Renderer *r ) const
{
	OpenGL::Selection::Name name( _id );

	OpenGL::Math::dvec3 o  = r->origin();
	OpenGL::Math::box3 box = _rect.translated(-o);

	glBegin( GL_TRIANGLE_STRIP );
		glVertex2d( box.minPoint().x, box.maxPoint().y );
		glVertex2d( box.minPoint().x, box.minPoint().y );
		glVertex2d( box.maxPoint().x, box.maxPoint().y );
		glVertex2d( box.maxPoint().x, box.minPoint().y );
	glEnd();
}

// ------------------------------------------------------------------------------------------------
bool VRectangle::intersects( const OpenGL::Math::box3 &b ) const
{
	return _rect.overlaps( b );
}
